<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ProductGroupMenuElement
 *
 * @author aneto
 */
class ProductGroupMenuElement extends MenuElement{

	private $subElements = array();

	public function __construct($data, NavigationMenu $e, MenuElement $me = null){
		parent::__construct($data, $e, $me);

		$this->lft = isset($data['lft']) && !empty($data['lft']) ?  $data['lft'] : '';
		$this->rgt = isset($data['rgt']) && !empty($data['rgt']) ?  $data['rgt'] : '';


		$this->link  = LinkGen::getInstance(LinkGen::PRODUCT_GROUP)->link($data['ProductGroup']['id']);
	}


	public function getLinkData(){
		//return array_merge(array('c' . $this->getLevel()  => $this->getSlug()), parent::getLinkData());
		//return array_merge(parent::getLinkData(), array('c1'   => $this->getSlug()));
		
	}


	public function getType(){
		return MenuElement::PRODUCT_GROUP;
	}


	public function isHidden(){
		return  !((boolean)$this->getName());
		
	}


	public function isActive(){
		return $this->getMenu()->isActive($this);
	}


	public function hasChildren(){
		return $this->getSubElements();
	}


	public function getSubElements(){
		if($this->subElements)return $this->subElements;

		$data = $this->getFetchedData();

		//product groups could be after elements
		$q = Doctrine_Query::create()
		->from('ProductGroup pg')
		->innerJoin('pg.Translation t WITH t.lang = ? AND t.name != ""', array($this->getMenu()->getCulture()))
		->setHydrationMode(Doctrine::HYDRATE_ARRAY)
		->where('pg.lft > ? AND pg.rgt < ? AND pg.level = ?', array($this->getLft(), $this->getRgt(),$data['ProductGroup']['level']+1))
		->orderBy('pg.lft asc');


		foreach($q->execute() as $child){
			$pass = array('ProductGroup' => $child);
			$this->subElements[] =  MenuElement::getInstance($pass, MenuElement::PRODUCT_GROUP, $this->getMenu(), $this);
		}

		return $this->subElements;
	}


	public function getLft(){
		$data = $this->getFetchedData();
		return $data['ProductGroup']['lft'];
	}


	public function getRgt(){
		$data = $this->getFetchedData();
		return $data['ProductGroup']['rgt'];
	}


	public function getId(){
		$data = $this->getFetchedData();
		
		return $data['ProductGroup']['id'];
	}

	
	public function getName(){
		$data = $this->getFetchedData();
		
		return $data['ProductGroup']['Translation'][$this->getMenu()->getCulture()]['name'];
	}


	protected function getSlug(){
		$data = $this->getFetchedData();
		return $data['ProductGroup']['Translation'][$this->getMenu()->getCulture()]['slug'];
	}


	protected function getLevel(){
		$data = $this->getFetchedData();
		return $data['ProductGroup']['level'];
	}

	
	public function getParameter($p = 'parameter'){
		$data = $this->getFetchedData();
		return $data['ProductGroup'][$p];
	}

	
}